package com.zcxy360.sandtable.controller;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zcxy360.sandtable.domain.StageUnit;
import com.zcxy360.sandtable.service.StageUnitService;

/**
 * 课程小节接口
 * @author 盛亚辉
 *
 */
@RequestMapping("course/stage/unit")
@RestController
public class StageUnitController {

	@Autowired
	private StageUnitService stageUnitService;

	/**
	 * 获取小节
	 * @param unitId 小节id
	 * @return 小节对象
	 */
	@RequiresAuthentication
	@GetMapping("{unitId}")
	public Object getByUnitId(@PathVariable("unitId") Integer unitId) {
		return stageUnitService.getByUnitId(unitId);
	}
	
	/**
	 * 获取小节集合
	 * @param stageId 阶段小节id
	 * @return 小节集合
	 */
	@RequiresAuthentication
	@GetMapping
	public Object listByStageId(@RequestParam("stageId") Integer stageId) {
		return stageUnitService.listByStageId(stageId);
	}
	
	/**
	 * 新增小节
	 * @param unitJson 小节json字符串
	 * @return 小节集合
	 */
	@RequiresRoles("teacher")
	@PostMapping
	public Object post(@RequestParam("unitJson") String unitJson) {
		return stageUnitService.post(unitJson);
	}
	
	/**
	 * 更新小节
	 * @param courseUnit 小节字段
	 * @return 布尔值
	 */
	@RequiresRoles("teacher")
	@PutMapping
	public Object update(StageUnit courseUnit) {
		return stageUnitService.update(courseUnit);
	}
	
	/**
	 * 删除小节
	 * @param unitIds 小节id数组
	 * @return 布尔值
	 */
	@RequiresRoles("teacher")
	@DeleteMapping("{unitIds}")
	public Object delete(@PathVariable("unitIds") int[] unitIds) {
		return stageUnitService.delete(unitIds);
	}
	
	/**
	 * 小节排序
	 * @param unitId1 原始小节id
	 * @param unitId2 目标小节id
	 * @return 布尔值
	 */
	@RequiresRoles("teacher")
	@PutMapping("sort")
	public Object sort(@RequestParam("unitId1") Integer unitId1,@RequestParam("unitId2") Integer unitId2) {
		return stageUnitService.sort(unitId1,unitId2);
	}
	
}
